﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using WatiN.Core;
using WebAutomation.Browsers;

namespace WebAutomation
{
    public class ButtonCommand : CommandBase
    {
        public ButtonCommand(WebBrowserObject browserObject)
        {
            this.Initialize(browserObject);

        }
        public override bool ExecuteCommand(WebBrowserObject browserObject, string[] parameter, string url, string testRepo, string testResultLocation, string dbConnString, ref DataSet dataFromDB, ref TestLineResult testLineResult, ref bool stopTest, WebAutoUtils.ITestDataExceptionRules testDataExceptionRules)
        {
            WebBrowserObject.ElementProperties Button = new WebBrowserObject.ElementProperties();
            string command = parameter[0];
            testLineResult.LineCommand = command;
            testLineResult.LineParameter = parameter[1];
            testLineResult.LineSubCommand = command;
            if (this.BrowserObject == null)
            {
                this.BrowserObject = browserObject;
            }
            try
            {

                switch (command.ToLower())
                {
                    case "click":

                        Button = GetButton(parameter[1]);
                        if (Button.Exists)
                        {
                            ButtonClick(Button.Element);
                            testLineResult.LinePassed = true;
                        }else
                        {
                            testLineResult.LineMessage = "Could not find Button :" + parameter[1];
                            testLineResult.LinePassed = false;
                        }
                        break;
                    case "mouseup":

                        Button = GetButton(parameter[1]);
                        if (Button.Exists)
                        {
                            ButtonMouseUp(Button.Element);
                            testLineResult.LinePassed = true;
                        }
                        else
                        {
                            testLineResult.LineMessage = "Could not find Button :" + parameter[1];
                            testLineResult.LinePassed = false;
                        }
                        break;

                    case "mousedown":

                        Button = GetButton(parameter[1]);
                        if (Button.Exists)
                        {
                            ButtonMouseDown(Button.Element);
                            testLineResult.LinePassed = true;
                        }
                        else
                        {
                            testLineResult.LineMessage = "Could not find Button :" + parameter[1];
                            testLineResult.LinePassed = false;
                        }
                        break;
                    
                }






            }
            catch (Exception ex)
            {
                testLineResult.IsFrameworkError = true;
                testLineResult.LineMessage = "Error." + ex.Message + "." + ex.StackTrace;
                testLineResult.LinePassed = false;
            }

            return testLineResult.LinePassed;
        }

        public void ButtonClick(object buttonField)
        {
            BrowserObject.ButtonClick(buttonField);
        }

        public void ButtonMouseUp(object buttonField)
        {
            BrowserObject.ButtonMouseUp(buttonField);
        }

        public void ButtonMouseDown(object buttonField)
        {
            BrowserObject.ButtonMouseDown(buttonField);
        }

        public WebBrowserObject.ElementProperties GetButton(string valueOrIdOrName)
        {
            valueOrIdOrName = valueOrIdOrName.Replace("\r\n", "");
            WebBrowserObject.ElementProperties Button = new WebBrowserObject.ElementProperties();
            try
            {
                Button = this.BrowserObject.Button(Find.ById(valueOrIdOrName));
                if (!Button.Exists)
                {
                    Button = this.BrowserObject.Button(Find.ByName(valueOrIdOrName));
                }
                if (!Button.Exists)
                {
                    Button = this.BrowserObject.Button(Find.ByValue(valueOrIdOrName));
                    if ( !Button.Exists)
                    {
                        Button = this.BrowserObject.Button(Find.ById(valueOrIdOrName));
                    }
                }
                if (!Button.Exists)
                {
                    Button = this.BrowserObject.Button(Find.ByClass(valueOrIdOrName));
                }

                if ( !Button.Exists)
                {
                    Button = _getButtonFromIFrame(this.BrowserObject, valueOrIdOrName);
                }
              
            }
            catch (UnauthorizedAccessException ex) { }
            if (!Button.Exists)
            {
                Button = _getButtonFromIFrame(this.BrowserObject, valueOrIdOrName);
            }
            return Button;
        }

        private WebBrowserObject.ElementProperties _getButtonFromIFrame(WebBrowserObject browserObject, string valueOrIdOrName)
        {
            WebBrowserObject.ElementProperties btn = new WebBrowserObject.ElementProperties() ;
            try
            {
                if (browserObject.Frames().Count > 0)
                {
                    for (int i = 0; i <= browserObject.Frames().Count-1; i++)
                    {
                        browserObject.WaitForCompleteIFrame(i, 5000);
                        btn = browserObject.ButtonIFrame(i,Find.ByName(valueOrIdOrName));
                        if (!btn.Exists)
                        {
                            btn = browserObject.ButtonIFrame(i, Find.ById(valueOrIdOrName));
                        }
                        if (!btn.Exists)
                        {
                            btn = browserObject.ButtonIFrame(i, Find.ByClass(valueOrIdOrName));
                        }
                        if ( btn.Exists)
                        {
                            break;
                        }
                    }

                }
               
            }
            catch (UnauthorizedAccessException ex)
            {

            }

            return btn;
        }
    }
}
